package com.android.gilastv.ui.activity;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.baselibrary.interface_.CommCallBack;
import com.android.baselibrary.interface_.OkHttpCallBack;
import com.android.baselibrary.responce.BaseResponce;
import com.android.baselibrary.tool.CommToast;
import com.android.baselibrary.util.Util;
import com.android.baselibrary.view.GridSpacingItemDecoration;
import com.android.gilastv.R;
import com.android.gilastv.adapter.CommVideoListAdapter;
import com.android.gilastv.adapter.MenuListAdapter;
import com.android.gilastv.bean.CommVideoInfo;
import com.android.gilastv.bean.MenuInfo;
import com.android.gilastv.constants.Constants;
import com.android.gilastv.manager.API_Manager;
import com.android.gilastv.ui.view.Comm_HeadStateView;

import java.util.List;

import butterknife.BindView;

public class SearchActivity extends BaseActivity {

    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.recyclerview_menu)
    RecyclerView recyclerview_menu;
    @BindView(R.id.head_stateview)
    Comm_HeadStateView head_stateview;

    List<CommVideoInfo> list;
    int page=1;
    int size=30;

    CommVideoListAdapter adapter;
    MenuListAdapter menuListAdapter;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_search;
    }

    @Override
    protected void initView() {
        head_stateview.setInputCallback(new CommCallBack() {
            @Override
            public void onResult(Object obj) {
                queryVideos();
            }
        });
        head_stateview.setLeftClickFinish(this);

        //视频列表
        recyclerview.setLayoutManager(new GridLayoutManager(mContext, 5));
        recyclerview.addItemDecoration(new GridSpacingItemDecoration(5, Util.dip2px(mContext, 10), true));
        adapter = new CommVideoListAdapter(mContext, null);
        recyclerview.setAdapter(adapter);
//        list = CommVideoInfo.getTestData();
        adapter.setData(list);

        //菜单
        recyclerview_menu.setLayoutManager(new LinearLayoutManager(mContext));
        menuListAdapter = new MenuListAdapter(mContext, callBack);
        recyclerview_menu.setAdapter(menuListAdapter);
//        menuListAdapter.setData(MenuInfo.getList());
    }

    CommCallBack callBack=new CommCallBack() {
        @Override
        public void onResult(Object obj) {
            queryVideos();
        }
    };

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        //获取视频分类
        API_Manager.videoTypesList(mContext, new OkHttpCallBack<BaseResponce<List<MenuInfo>>>() {
            @Override
            public void onSuccess(BaseResponce<List<MenuInfo>> baseResponce) {
                menuListAdapter.setData(baseResponce.getData());
                queryVideos();
            }

            @Override
            public void onFailure(BaseResponce<List<MenuInfo>> baseResponce) {
                CommToast.showToast(mContext, Constants.NetError);
            }
        });
    }

    private void queryVideos() {
        String params = head_stateview.et_content.getText();
        String type = menuListAdapter.getSelect().dataId;
        //获取视频分类
        API_Manager.queryVideos(mContext,  page,  size,  params,  type,new OkHttpCallBack<BaseResponce<List<CommVideoInfo>>>() {
            @Override
            public void onSuccess(BaseResponce<List<CommVideoInfo>> baseResponce) {
                adapter.setData(baseResponce.getData());
            }

            @Override
            public void onFailure(BaseResponce<List<CommVideoInfo>> baseResponce) {
                CommToast.showToast(mContext, Constants.NetError);
            }
        });
    }

    //    @OnClick({R.id.tv_sendcode})
//    public void onViewClicked(View view) {
//        switch (view.getId()) {
//            case R.id.tv_sendcode:
//                break;
//        }
//    }
}
